<?php

namespace App\Http\Controllers\App;

use AlibabaCloud\Client\AlibabaCloud;
use App\Http\Controllers\Controller;

use App\Http\Requests\App\VodPlayInfoRequest;
use Illuminate\Http\Request;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
use AlibabaCloud\Vod\Vod;

class VodPlayInfoController extends Controller
{
    const VOD_CLIENT_NAME = 'DEMO';

    protected function initVodClient()
    {
        $regionId = 'cn-shanghai';
        AlibabaCloud::accessKeyClient(env('ALIYUN_ACCESS_KEY_ID'), env('ALIYUN_ACCESS_KEY_SECRET'))
            ->regionId($regionId)
            ->connectTimeout(1)
            ->timeout(3)
            ->name(self::VOD_CLIENT_NAME);
    }

    /**
     * Handle the incoming request.
     *
     * @param VodPlayInfoRequest $request
     * @return \Illuminate\Http\Response
     * @throws ClientException
     * @throws ServerException
     */
    public function __invoke(VodPlayInfoRequest $request)
    {
        $this->initVodClient();
        $playInfo = Vod::v20170321()->getPlayInfo()->client(self::VOD_CLIENT_NAME)
            ->withVideoId($request->video_id)// 指定接口参数
            ->withAuthTimeout(3600 * 24)
            ->format('JSON')// 指定返回格式
            ->request();      // 执行请求
        return json_encode($playInfo->PlayInfoList);
    }
}
